package com.cn.meet.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cn.meet.common.R;
import com.cn.meet.entity.Notice;
import com.cn.meet.entity.Reserve;
import com.cn.meet.entity.Room;
import com.cn.meet.service.NoticeService;
import com.cn.meet.service.ReserveService;
import com.cn.meet.service.RoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @Autowired
    private ReserveService reserveService;

    @Autowired
    private RoomService roomService;

    // 分页查询通知
    @GetMapping("/page")
    public R<Page<Notice>> page(int page, int pageSize, HttpServletRequest request) {
        //分页构造器
        Page<Notice> pageInfo = new Page<>(page, pageSize);
        //条件构造器
        HttpSession session = request.getSession();
        Object userid = session.getAttribute("user");

        if(userid == null) {
            return R.error("为获取到用户信息");
        }

        LambdaQueryWrapper<Notice> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(Notice::getUserid, userid);

        //添加排序条件，根据sendTime进行排序
        queryWrapper.eq(Notice::getUserid, userid).orderByDesc(Notice::getSendTime);

        //分页查询
        noticeService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    // 根据勾选的用户id插入一条通知
    @PostMapping("/sendNotice")
    public R<String> sendNotice(Long id) {
        // 先根据勾选的用户id查询对应的预订信息，获取预订信息实体
        LambdaQueryWrapper<Reserve> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Reserve::getUserid, id)
                        .eq(Reserve::getStatus, 1);
        Reserve reserve = reserveService.getOne(queryWrapper);

        if(reserve == null) {
            return R.error("未找到该用户的有效预订信息");
        }

        // 再根据预订的id查询对应申请的会议室,获取会议室实体
        LambdaQueryWrapper<Room> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Room::getId, reserve.getRoomid());
        Room room = roomService.getOne(queryWrapper1);

        if(room == null) {
            return R.error("未找到有效的会议室信息");
        }

        // 设置通知实体信息
        String loc = room.getLocation();
        Notice notice = new Notice();
        notice.setUserid(id);
        if(reserve.getStatus() == 1){
            notice.setContent("您请求的：" + loc + "会议室审核已通过");
        }else if (reserve.getStatus() == 2){
            notice.setContent("您请求的" + loc + "会议室审核为通过");
        }
        notice.setSendTime(LocalDateTime.now());

        noticeService.save(notice);
        return R.success("已通知!");
    }
}
